// pages/test/redPackage/redPackage.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:0,
    count:'',
    flag:true,
    moneyFlag:true,
    countFlag:true
  },
  //输入个数
  enterCount:function(e){
    if (e.detail.value>100){
      this.setData({
        count:100,
        countFlag:false
      });
    } else if (e.detail.value == 0 || e.detail.value.trim() == ''){
      this.setData({
        count: 0,
        countFlag: true
      });
    }else{
      this.setData({
        count: e.detail.value,
        countFlag: false
      });
    }
    this.flagFun();
  },
  //输入金额
  enterMoney:function(e){
    if (e.detail.value == 0 || e.detail.value.trim() == ''){
      this.setData({
        money: 0,
        moneyFlag: true
      });
    } else{
      this.setData({
        money: parseFloat(e.detail.value.trim()).toFixed(2),
        moneyFlag: false
      });
    };
    this.flagFun();
  },
  flagFun:function(){
    console.log(this.data);
    if (this.data.moneyFlag==false && this.data.countFlag==false){
      this.setData({
        flag:false
      })
    }else{
      this.setData({
        flag: true
      })
    }
  },
  //红包发送
  giveRedPackage:function(){
    let that =this;
    wx.showLoading();
    wx.request({
      url: 'https://wx.sencegroup.com/xingchen/api/zbds/redpacket/sendRedpacket',
      data:{
        token: app.globalData.token,
        shop_live_id: that.data.id,
        redpacket_count:that.data.count,
        redpacket_total_balance:that.data.money
      },
      method:'POST',
      dataType:'json',
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
        console.log(res);
        wx.hideLoading();
        if(res.data.code==0){
          wx.showActionSheet({
            itemList: ['余额支付', '微信支付'],
            success: function (result) {
              console.log(result);
              if (result.tapIndex==0){
                that.payBalance(res.data.data.redpacket_id);
              } else if (result.tapIndex == 1){
                that.payWx(res.data.data.redpacket_id,'赠送红包')
              }
            }
          })  
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  payWx: function (param,subject){
    wx.request({
      url: "https://wx.sencegroup.com/xingchen/api/payment/weixinpay/h5/makePreOrder",
      data: {
        token: app.globalData.token,
        redpacket_id:param,
        subject: subject,
        pay_type:1,
        xcx_openId: app.globalData.openId
      },
      method: 'POST',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (answer) {
        console.log(answer);
        if(answer.data.code==0){
          wx.requestPayment({
            timeStamp: answer.data.data.timeStamp,
            nonceStr: answer.data.data.nonceStr,
            package: answer.data.data.packageValue,
            signType: answer.data.data.signType,
            paySign: answer.data.data.sign,
            success:function(){
              wx.showToast({
                title: '支付成功',
                icon:'success',
                mask:true,
              });
              setTimeout(()=>{
                wx.navigateBack();
              },1000)
            },
            fail:function(){
              wx.showToast({
                title: '支付失败',
                icon: 'none'
              })
            }
          });
        } else {
          wx.showToast({
            title: answer.data.msg,
          })
        }
      }
    })
  },
  payBalance:function(param){
    console.log(111);
    let that=this;
    wx.request({
      url: 'https://wx.sencegroup.com/xingchen/api/zbds/redpacket/payRedpacket',
      data:{
        token: app.globalData.token,
        redpacket_id:param,
        live_id: that.data.id
      },
      method:'POST',
      header:{
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:function(res){
        console.log(res);
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
        if(res.data.code==0){
          setTimeout(() => {
            wx.navigateBack();
          }, 1000)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})